//
//  OrderListTableViewCell.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/9.
//

#import "OrderListTableViewCell.h"

@implementation OrderListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        self.iconV = [[UIImageView alloc] init];
        [self.contentView addSubview:self.iconV];
        [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.top.equalTo(self.contentView.mas_top);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        
        self.topLab = [Utility createLabel:[UIFont fontWithName:kFutura size:13] color:[UIColor clearColor]];
        [self.iconV addSubview:self.topLab];
        self.topLab.textAlignment = 1;
        [self.topLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconV.mas_left);
            make.top.equalTo(self.iconV.mas_top);
            make.right.equalTo(self.iconV.mas_right);
            make.height.equalTo(@30);
        }];
        
        self.backV = [[UIView alloc] init];
        [self.iconV addSubview:self.backV];
        self.backV.backgroundColor = [UIColor whiteColor];
        [self.backV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconV.mas_left).offset(10);
            make.right.equalTo(self.iconV.mas_right).offset(-10);
            make.top.equalTo(self.iconV.mas_top).offset(50);
            make.bottom.equalTo(self.iconV.mas_bottom).offset(-20);
        }];
        self.backV.layer.cornerRadius = 10;
        self.backV.layer.masksToBounds = YES;
        
        self.tipBtn = [[UIButton alloc] init];
        self.tipBtn.titleLabel.font = [UIFont fontWithName:kFutura size:12];
       
        [self.tipBtn setTitleColor:[UIColor colorWithHexString:@"#FF461A"] forState:UIControlStateNormal];
        [self.tipBtn setTitleColor:[UIColor colorWithHexString:@"#FF461A"] forState:UIControlStateSelected];
        [self.tipBtn setTitleColor:[UIColor colorWithHexString:@"#FF461A"] forState:UIControlStateHighlighted];
        
        [self.iconV addSubview:self.tipBtn];
        [self.tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconV.mas_left).offset(10);
            make.right.equalTo(self.iconV.mas_right).offset(-10);
            make.bottom.equalTo(self.iconV.mas_bottom).offset(0);
            make.height.equalTo(@40);
        }];
        
        self.tipBtn.hidden = YES;
        
        
        
        
        
        self.logoV = [[UIImageView alloc] init];
        [self.backV addSubview:self.logoV];
        [self.logoV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backV.mas_left).offset(10);
            make.width.with.height.equalTo(@30);
            make.top.equalTo(self.backV.mas_top).offset(15);
        }];
        
        self.nameLab = [Utility createLabel:[UIFont fontWithName:kFutura size:14] color:[UIColor colorWithHexString:@"#022D06"]];
        [self.backV addSubview:self.nameLab];
        [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.logoV.mas_right).offset(5);
            make.right.equalTo(self.backV.mas_right).offset(-120);
            make.height.equalTo(@30);
            make.centerY.equalTo(self.logoV.mas_centerY);
        }];
        
        
        self.dateLab = [Utility createLabel:[UIFont fontWithName:kFutura size:13] color:[UIColor colorWithHexString:@"#638473"]];
        [self.backV addSubview:self.dateLab];
        [self.dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.backV.mas_right).offset(-10);
            make.height.equalTo(@30);
            make.centerY.equalTo(self.logoV.mas_centerY);
        }];
        
        self.lineV = [[UIView alloc] init];
        [self.backV addSubview:self.lineV];
        self.lineV.backgroundColor = kLineColor;
        [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.logoV.mas_left);
            make.right.equalTo(self.dateLab.mas_right);
            make.height.equalTo(@1);
            make.top.equalTo(self.logoV.mas_bottom).offset(15);
        }];
        
        
        self.priceLab = [Utility createLabel:[UIFont fontWithName:kFuturaBold size:20] color:[UIColor colorWithHexString:@"#022D06"]];
        [self.backV addSubview:self.priceLab];
        [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.logoV.mas_left);
            make.top.equalTo(self.lineV.mas_bottom);
            make.right.equalTo(self.backV.mas_right).offset(-80);
            make.bottom.equalTo(self.backV.mas_bottom);
        }];
        self.priceLab.numberOfLines = 2;
        

        UIImage *imgB = [UIImage imageNamed:@"icon_orderApply2"];
        self.orderBtn = [[UIButton alloc] init];
        self.orderBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
        [self.orderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.backV addSubview:self.orderBtn];
        [self.orderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(imgB.size.width));
            make.height.equalTo(@(imgB.size.height));
            make.right.equalTo(self.backV.mas_right).offset(-15);
            make.centerY.equalTo(self.priceLab.mas_centerY);
        }];
        self.orderBtn.hidden = YES;
        
        
    }
    return self;
}

- (void)setModel:(OrderModel *)model{
    _model = model;
    self.topLab.text = esString(model.rejoining.shareday);
    
    [self.logoV sd_setImageWithURL:[NSURL URLWithString:esString(model.rejoining.generations)] placeholderImage:[UIImage imageNamed:@"icon_logo"] options:SDWebImageRetryFailed];
    
    self.nameLab.text = esString(model.rejoining.harness);
    
    self.priceLab.text = [NSString stringWithFormat:@"%@\n%@",esString(model.rejoining.distinctionday),esString(model.rejoining.followday)];
    [self.priceLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:13]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#638473"]}] forStringArray:@[esString(model.rejoining.followday),esString(model.rejoining.followday)]];
    
    self.orderBtn.hidden = YES;
    
    self.tipBtn.hidden = YES;
    [self.tipBtn setImage:[UIImage imageNamed:@"icon_orderTips"] forState:UIControlStateNormal];
    [self.tipBtn setImage:[UIImage imageNamed:@"icon_orderTips"] forState:UIControlStateSelected];
    [self.tipBtn setImage:[UIImage imageNamed:@"icon_orderTips"] forState:UIControlStateHighlighted];
    [self.tipBtn setTitle:[NSString stringWithFormat:@" %@",esString(model.rejoining.paragraphed)] forState:UIControlStateNormal];
    [self.tipBtn setTitle:[NSString stringWithFormat:@" %@",esString(model.rejoining.paragraphed)] forState:UIControlStateSelected];
    [self.tipBtn setTitle:[NSString stringWithFormat:@" %@",esString(model.rejoining.paragraphed)] forState:UIControlStateHighlighted];

    if (!PMStringIsEmpty(model.rejoining.paragraphed)) {
        self.tipBtn.hidden = NO;
    }
    
    self.dateLab.text = [esString(model.rejoining.margin) stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    
    [self.orderBtn setTitle:esString(model.rejoining.activities) forState:UIControlStateNormal];
    [self.orderBtn setTitle:esString(model.rejoining.activities) forState:UIControlStateSelected];
    [self.orderBtn setTitle:esString(model.rejoining.activities) forState:UIControlStateHighlighted];
    
    if (!PMStringIsEmpty(model.rejoining.paragraphed)) {
        [self.backV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconV.mas_left).offset(10);
            make.right.equalTo(self.iconV.mas_right).offset(-10);
            make.top.equalTo(self.iconV.mas_top).offset(50);
            make.bottom.equalTo(self.iconV.mas_bottom).offset(-40);
        }];
    } else {
        [self.backV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconV.mas_left).offset(10);
            make.right.equalTo(self.iconV.mas_right).offset(-10);
            make.top.equalTo(self.iconV.mas_top).offset(50);
            make.bottom.equalTo(self.iconV.mas_bottom).offset(-20);
        }];
    }
    
    
    //Apply
    if ([model.rejoining.wide integerValue] == 3) {
        self.iconV.image = [UIImage imageNamed:@"icon_orderApply"];
        self.topLab.textColor = [UIColor colorWithHexString:@"#FFB126"];
        
        self.orderBtn.hidden = NO;
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderApply2"] forState:UIControlStateNormal];
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderApply2"] forState:UIControlStateSelected];
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderApply2"] forState:UIControlStateHighlighted];
    }
    //Finish
    else if ([model.rejoining.wide integerValue] == 5) {
        self.iconV.image = [UIImage imageNamed:@"icon_orderFinish"];
        self.topLab.textColor = [UIColor colorWithHexString:@"#638473"];
    }
    //Repayment
    else if ([model.rejoining.wide integerValue] == 2) {
        self.topLab.textColor = [UIColor colorWithHexString:@"#FF8700"];
        self.iconV.image = [UIImage imageNamed:@"icon_orderRepay"];
        
        self.orderBtn.hidden = NO;
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderRepay2"] forState:UIControlStateNormal];
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderRepay2"] forState:UIControlStateSelected];
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderRepay2"] forState:UIControlStateHighlighted];
    }
    //Delay
    else if ([model.rejoining.wide integerValue] == 1) {
        self.iconV.image = [UIImage imageNamed:@"icon_orderDelay"];
        self.topLab.textColor = [UIColor colorWithHexString:@"#FE3A2F"];
        
        self.orderBtn.hidden = NO;
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderDelay2"] forState:UIControlStateNormal];
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderDelay2"] forState:UIControlStateSelected];
        [self.orderBtn setBackgroundImage:[UIImage imageNamed:@"icon_orderDelay2"] forState:UIControlStateHighlighted];
    }
    //Review
    else if ([model.rejoining.wide integerValue] == 4) {
        self.iconV.image = [UIImage imageNamed:@"icon_orderFinish"];
        self.topLab.textColor = [UIColor colorWithHexString:@"#00BA60"];
    }
    
    
}

+ (CGFloat)getCellHeight:(OrderModel *)mm{
    UIImage *img1 = [UIImage imageNamed:@"icon_orderDelay"];
    UIImage *img2 = [UIImage imageNamed:@"icon_orderFinish"];
    
//    if ([mm.rejoining.cattle integerValue] == 180 || [mm.rejoining.cattle integerValue] == 174) {
    if (!PMStringIsEmpty(mm.rejoining.paragraphed)) {
        return DimensionBaseIphone6(img1.size.height);
    }
    return DimensionBaseIphone6(img2.size.height);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
